package com.thexb.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thexb.sys.entity.Menu;
import com.thexb.sys.mapper.MenuMapper;
import com.thexb.sys.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thexb
 * @since 2023-04-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> getAllMenu() {
        // 获取父级菜单
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId, 0);
        List<Menu> list = this.list(wrapper);
        for (Menu menu : list) {
            // 查询子菜单
            LambdaQueryWrapper<Menu> subWrapper = new LambdaQueryWrapper<>();
            subWrapper.eq(Menu::getParentId, menu.getMenuId());
            List<Menu> children = this.list(subWrapper);
            menu.setChildren(children);
        }
        return list;
    }

    @Override
    public List<Menu> getLoginMenu(int user_id) {
        List<Menu> loginMenu = this.baseMapper.getLoginMenu(user_id, 0);
        if(loginMenu != null) {
            for (Menu menu : loginMenu) {
                List<Menu> subMenu = this.baseMapper.getLoginMenu(user_id, menu.getMenuId());
                menu.setChildren(subMenu);
            }
        }
        return loginMenu;
    }
}
